﻿using System;

using Moe.ECamel.Common.Entities;
using Moe.ECamel.Client.Controllers;
using Moe.ECamel.Client.WinForms.Common;
using Moe.ECamel.Common.Security;

namespace Moe.ECamel.Client.WinForms.Controls
{
	public partial class LibraryDetailsControl : ECamelUserControl
	{
		private EntityDetailsController<Library> _controller;
		public LibraryDetailsControl()
		{
			InitializeComponent();
		}

		public void Initialize(EntityDetailsController<Library> detailsController)
		{
			this._controller = detailsController;
			RefreshPermissions();
			Bind(detailsController.Entity);
		}

		private void RefreshPermissions()
		{
			if (ECamelHelper.IsPermitted(Operations.LibraryEdit))
			{
				switch (_controller.Mode)
				{
					case DetailsMode.View:
						EditCancelButton.Visible = true;
						EditCancelButton.Text = "עריכה";
						EditConfirmButton.Visible = false;
						break;
					case DetailsMode.Edit:
						EditCancelButton.Visible = true;
						EditCancelButton.Text = "ביטול";
						EditConfirmButton.Visible = true;
						EditConfirmButton.Text = "אישור";
						break;
					case DetailsMode.Create:
						EditCancelButton.Visible = false;
						EditConfirmButton.Visible = false;
						break;
				}				
			}
			else
			{
				EditCancelButton.Visible = EditConfirmButton.Visible = false;
			}

			LibraryNameTextBox.ReadOnly = (_controller.Mode == DetailsMode.View);
			AddressTextBox.ReadOnly = (_controller.Mode == DetailsMode.View);
			TelephoneTextBox.ReadOnly = (_controller.Mode == DetailsMode.View);
			FaxTextBox.ReadOnly = (_controller.Mode == DetailsMode.View);
			OpeningHoursTextBox.ReadOnly = (_controller.Mode == DetailsMode.View);
		}
		private void Bind(Library entity)
		{
			this.LibraryBindingSource.DataSource = entity;
			LibraryIdTextBox.Bind(this.LibraryBindingSource, "LibraryId");
			LibraryTypeTextBox.Bind(this.LibraryBindingSource, "LibraryType.LibraryTypeName");
			LibraryNameTextBox.Bind(this.LibraryBindingSource, "LibraryName");
			ParentLibraryTextBox.Bind(this.LibraryBindingSource, "ParentLibraryName");
			AddressTextBox.Bind(this.LibraryBindingSource, "Address");
			TelephoneTextBox.Bind(this.LibraryBindingSource, "Telephone");
			FaxTextBox.Bind(this.LibraryBindingSource, "Fax");
			OpeningHoursTextBox.Bind(this.LibraryBindingSource, "OpeningHours");
			ActiveCheckBox.Bind(this.LibraryBindingSource, "Active");
		}
		public void CancelEdit()
		{
			this.LibraryBindingSource.CancelEdit();
		}

		private void EditCancelButton_Click(object sender, EventArgs e)
		{
			if (_controller.Mode == DetailsMode.View)
				_controller.Mode = DetailsMode.Edit;
			else if (_controller.Mode == DetailsMode.Edit)
			{
				_controller.Mode = DetailsMode.View;
				CancelEdit();
			}
			RefreshPermissions();
		}
		private void EditConfirmButton_Click(object sender, EventArgs e)
		{
			var error = _controller.SubmitChanges();
			if (error != null)
				ECamelHelper.ShowMessage(error);
			else
			{
				_controller.Mode = DetailsMode.View;
				//_controller.Entity = _controller.Controller.
				RefreshPermissions();
			}
		}
	}
}
